package com.dearbinge.opentapi.content.userSolicitude;

import com.dearbinge.data.api.userSolicitude.UserSolicitudeService;
import com.dearbinge.data.pojo.userSolicitude.UserSolicitude;
import com.dearbinge.utils.PageResult;
import com.dearbinge.utils.ResultMsg;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;
import java.util.Map;

@Api(description = "用户关注")
@RestController
@RequestMapping("/userSolicitudeController")
@EnableAutoConfiguration
public class userSolicitudeController extends WebMvcConfigurerAdapter {

    @Autowired
    private UserSolicitudeService userSolicitudeService;

    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "/addOrUpdate",method =  RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody UserSolicitude userSolicitude){
        try {
            System.out.println("**********************被关注用户ID："+userSolicitude.getSolicitudeId()+"关注用户ID："+userSolicitude.getUserId()+"************************************");
            if (userSolicitude.getUserId().equals(userSolicitude.getSolicitudeId())){
                return ResultMsg.error(500,"对不起，不能自己关注自己！");
            }
            Integer sId = userSolicitudeService.getByUserIdAndSolicitudeId(userSolicitude.getUserId(),userSolicitude.getSolicitudeId());
            if (sId != null){
                userSolicitude.setId(sId);
                userSolicitude.setSolicitudeType(true);
            }
            if (userSolicitude.getId() != null && userSolicitude.getId() != 0){
                Integer dataCode = userSolicitudeService.update(userSolicitude);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else {
                String id =  userSolicitudeService.getIsSolicitude(userSolicitude.getUserId(),userSolicitude.getSolicitudeId());
                if (!StringUtils.isEmpty(id)){
                    return ResultMsg.error(500,"已经关注，无法再次关注！");
                }
                Integer dataCode = userSolicitudeService.insert(userSolicitude);
                if (dataCode > 0){
                    return ResultMsg.ok("新增成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID删除
     */
    @ApiOperation(value = "根据ID删除",notes = "根据ID删除")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id){
        try {
            Integer dataCode = userSolicitudeService.delete(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID查询
     */
    @ApiOperation(value = "根据ID查询",notes = "根据ID查询")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public ResultMsg<UserSolicitude> getById(Integer id){
        try {
            UserSolicitude userSolicitude = userSolicitudeService.getById(id);
            return ResultMsg.ok(userSolicitude);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据用户ID分页查询关注好友
     */
    @ApiOperation(value = "根据用户ID分页查询全部的关注好友",notes = "根据用户ID分页 查询关注好友")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query"),
            @ApiImplicitParam(name = "type",value = "类型（0：关注   1：粉丝）",paramType = "query"),
            @ApiImplicitParam(name = "pageNo",value = "当前页",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "页面数量",paramType = "query")})
    @RequestMapping(value = "/getByUserId",method = RequestMethod.GET)
    public PageResult<List<Map<String,Object>>> getByUserId(String userId,Integer type, Integer pageNo, Integer pageSize){
        PageResult<List<Map<String,Object>>> pageResult = new PageResult();
        try {
            pageResult = userSolicitudeService.getByUserId(userId,type,pageNo,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return pageResult;
    }

    /**
     * 取消关注
     */
    @ApiOperation(value = "取消关注",notes = "取消关注")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query"),
            @ApiImplicitParam(name = "solicitudeId",value = "被关注用户ID",paramType = "query")})
    @RequestMapping(value = "/cancelSolicitude",method = RequestMethod.GET)
    public ResultMsg<String> cancelSolicitude(String userId,String solicitudeId){
        try {
            Integer dataCode = userSolicitudeService.cancelSolicitude(userId,solicitudeId);
            if (dataCode > 0) {
                return ResultMsg.ok("取消成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据用户ID与别关注用户ID
     */
    @ApiOperation(value = "根据用户ID与别关注用户ID",notes = "根据用户ID与别关注用户ID")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query"),
            @ApiImplicitParam(name = "solicitudeId",value = "被关注用户ID",paramType = "query")})
    @RequestMapping(value = "/getIsSolicitude",method = RequestMethod.GET)
    public ResultMsg<String> getIsSolicitude(String userId,String solicitudeId){
        try {
            String id =  userSolicitudeService.getIsSolicitude(userId,solicitudeId);
            return ResultMsg.ok(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

}
